package com.serviceImpl;

import java.util.Date;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import com.dao.manager.PjinfoMapper;
import com.pojo.Pjinfo;
import com.service.PjInfoService;
import com.utils.UUIDUtils;
import com.utils.UploadUtils;

@Service
public class PjInfoServiceImp implements PjInfoService {

	@Resource
	private PjinfoMapper pjInfoMapper;
	@Value("${img.upload.path}")
	private String imgUploadPath;
	@Value("${img.access.path}")
	private String imgAccessPath;

	@Override
	public int addPjInfo(Map<String, String> paramsMap, MultipartFile[] pxtimg, MultipartFile[] jxtimg) {
		try {
			// 定义一个uuid
			String uuid = UUIDUtils.getUUID();
			List<String> pxtimgPaths = UploadUtils.uploadQiniuImg(pxtimg, imgUploadPath, imgAccessPath);
			List<String> jxtimgPaths = UploadUtils.uploadQiniuImg(jxtimg, imgUploadPath, imgAccessPath);
			Pjinfo entity = new Pjinfo();
			System.out.println(pxtimgPaths+"------------>pxtimgPaths");
			if (pxtimgPaths != null && !pxtimgPaths.isEmpty()) {
				entity.setPxtimg(pxtimgPaths.get(0));
				entity.setPxttwoimg(pxtimgPaths.get(1));
			} else {
				entity.setPxtimg("");
				entity.setPxttwoimg("");
			}
			if (jxtimgPaths != null && !jxtimgPaths.isEmpty()) {
				entity.setJxtimg(jxtimgPaths.get(0));
			} else {
				entity.setJxtimg("");
			}
			entity.setUuid(uuid);
			entity.setDeviceid(Integer.valueOf(paramsMap.get("deviceid")));
			entity.setPxtcode(paramsMap.get("pxtcode"));
			entity.setJxtcode(paramsMap.get("jxtcode"));
			entity.setCreatetime(new Date());
			pjInfoMapper.addPjInfo(entity);
			return 1;
		} catch (Exception e) {
			e.printStackTrace();
			return -1;
		}
	}

	@Override
	public List<Map<String, String>> getPjInfoByParam(Map<String, String> paramsMap) {
		String page = paramsMap.get("page");
		if (page == null || "".equals(page)) {
			page = "1";
		}
		String num = paramsMap.get("num");
		if (num == null || "".equals(num)) {
			num = "10";
			paramsMap.put("num", num);
		}
		int start = (Integer.parseInt(page) - 1) * Integer.parseInt(num);
		paramsMap.put("start", start + "");
		return pjInfoMapper.getPjInfoByParam(paramsMap);
	}

	@Override
	public int deletePjInfo(Map<String, String> paramsMap) {
		try {
			return pjInfoMapper.deletePjInfo(paramsMap);
		} catch (Exception e) {
			e.printStackTrace();
			return -2;
		}
	}

	@Override
	@Transactional
	public int updatePjInfo(Map<String, String> paramsMap, MultipartFile[] pxtimg, MultipartFile[] jxtimg) {
		try {
			// 定义一个uuid
			String uuid = paramsMap.get("uuid");
			List<String> pxtimgPaths = UploadUtils.uploadQiniuImg(pxtimg, imgUploadPath, imgAccessPath);
			List<String> jxtimgPaths = UploadUtils.uploadQiniuImg(jxtimg, imgUploadPath, imgAccessPath);
			Pjinfo entity = new Pjinfo();
			if (pxtimgPaths != null && !pxtimgPaths.isEmpty()) {
				entity.setPxtimg(pxtimgPaths.get(0));
				entity.setPxttwoimg(pxtimgPaths.get(1));
			} 
			if (jxtimgPaths != null && !jxtimgPaths.isEmpty()) {
				entity.setJxtimg(jxtimgPaths.get(0));
			} 
			entity.setUuid(uuid);
			entity.setPxtcode(paramsMap.get("pxtcode"));
			entity.setJxtcode(paramsMap.get("jxtcode"));
			entity.setCreatetime(new Date());
			pjInfoMapper.updatePjInfo(entity);
			return 1;
		} catch (Exception e) {
			e.printStackTrace();
			return -1;
		}
	}

	@Override
	public Map<String, String> getPjInfoByUuid(Map<String, String> paramsMap) {
		return pjInfoMapper.getPjInfoByUuid(paramsMap.get("id"));
	}

}
